<template>
  <div class="factory-card">
    <div class="factory-icon">🏭</div>
    <div class="factory-name">{{ factory.name }}</div>
    <div class="product-name">{{ factory.productName }}</div>
    <div class="warehouse-info">{{ factory.productNums }}/{{ factory.warehouseSize }}</div>
    <div class="progress-bar">
      <div class="progress-bar-inner" :style="{ width: factory.progress * 100 + '%' }"></div>
    </div>
    <button class="collect-btn" @click="collectResources">收集资源</button>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { collectFactoryResources } from '@/api/factory';
// 导入公共类型
import type { Factory } from '@/types/factory';


const props = defineProps<{
  factory: Factory;
}>();

const collecting = ref(false);

const collectResources = async () => {
  if (collecting.value) return;

  collecting.value = true;
  try {
    await collectFactoryResources(props.factory.id);
  } catch (error) {
    console.error('收集资源失败:', error);
  } finally {
    collecting.value = false;
  }
};
</script>

<style scoped>
/* 从templates/factory/list1.html复制并转换为scoped样式 */
.factory-card {
  background-color: #fff;
  border: 1px solid #ccc;
  border-radius: 10px;
  padding: 20px;
  width: 200px;
  text-align: center;
  margin: 10px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  position: relative;
}

.factory-icon {
  font-size: 50px;
}

.factory-name {
  font-weight: bold;
  margin-top: 10px;
  color: #333;
}

.product-name {
  margin-top: 5px;
  color: #555;
}

.warehouse-info {
  margin-top: 5px;
  color: #666;
}

.progress-bar {
  width: 100%;
  background-color: #f3f3f3;
  border-radius: 5px;
  margin-top: 10px;
  overflow: hidden;
}

.progress-bar-inner {
  height: 20px;
  border-radius: 5px;
  background-color: #4caf50;
  transition: width 0.5s ease-in-out;
}

.collect-btn {
  margin-top: 15px;
  padding: 8px 15px;
  background-color: #4caf50;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.collect-btn:hover {
  background-color: #45a049;
}
</style>
